package com.personal.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.personal.pojo.*;
import com.personal.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    //设置每页显示数据量
    private static final int PAGE_SIZE = 10;
    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(String username, String password, HttpSession session){
        TAdmin admin =  adminService.login(username,password);
        if (admin != null){
            //将登录信息放到session中
            session.setAttribute("logUser",admin);
            session.setAttribute("logUsername",admin.getTrole().getRolename());
            return "ok_ad";
        }else {
            return "no";
        }
    }
    //1.1登录信息验证成功跳转页面
    @RequestMapping("/toIndex")
    public String toIndex(){

        return "index";
    }
//2.会员信息管理,增删改查
    //2.1跳转到会员列表，将会员卡未到期的会员列表带上,弃用
    @RequestMapping("/toMemListPage")
    public String toMemListPage(Model model){
        List list = adminService.AllMem();
        model.addAttribute("memList",list);
        return "adminMem_list";
    }
    //2.1.1分页，显示第一页
    @RequestMapping("/splitOnepage")
    public String splitOnepage(Model model){
        PageInfo info = adminService.splitpage1(1,PAGE_SIZE);
        model.addAttribute("memInfo",info);
        return "adminMem_list";
    }
    //2.1.2分页ajax
    @RequestMapping("/ajaxsplit")
    @ResponseBody
    public void ajaxPage(int page, HttpSession session){
        //取得当前page参数页面数据
        PageInfo info = adminService.splitpage1(page,PAGE_SIZE);
        session.setAttribute("memInfo",info);
    }
    //2.1.3查询方法
    @RequestMapping("/adminFindMem")
    @ResponseBody
    public void adminFindMem(String datemin,String datemax,String memname, HttpSession session){
        PageInfo info = adminService.splitpage2(1,PAGE_SIZE,datemin,datemax,memname);
        //取得当前page参数页面数据
        session.setAttribute("memInfo",info);
    }
    //2.1.3查询方法分页
    @RequestMapping("/adminFindMemSplit")
    @ResponseBody
    public void adminFindMemSplit(int page,String datemin,String datemax,String memname, HttpSession session){
        PageInfo info = adminService.splitpage2(page,PAGE_SIZE,datemin,datemax,memname);
        //取得当前page参数页面数据
        session.setAttribute("memInfo",info);
    }
    //2.1.4添加会员
    @RequestMapping("/addNewMem")
    public String addNewMem(TMember member,Model model){
        int row = adminService.addNewMem(member);
        if (row==1){
            model.addAttribute("addMem","添加成功！");
            return "addSuccess";
        }else {
            model.addAttribute("addMem","添加失败！");
            return "adminMem_add";
        }
    }
    //2.1.5修改会员信息---显示原来的信息
    @RequestMapping("/editShow")
    @ResponseBody
    public void editShow(Integer memid,HttpSession session){
        TMember member = adminService.editShow(memid);
        session.setAttribute("editShowMem",member);
    }
    //2.1.5确定修改
    @RequestMapping("/editConfirm")
    public String editConfirm(TMember member,HttpSession session){
        //销毁session
        session.removeAttribute("editShowMem");
//        System.out.println(member);
        //修改方法
        int row = adminService.editMem(member);
//        System.out.println("row:"+row);
        return "adminMem-edit";
    }
    //2.1.6暂时删除
    @RequestMapping("/editByStates0")
    @ResponseBody
    public String editByStates0(Integer memid,HttpSession session){
        int row = adminService.editByStates0(memid);
        if (row==1){
            return "ok";
        }else {
            return "no";
        }
    }
    //2.1跳转到暂时删除会员信息列表
    @RequestMapping("/toDeMemListPage")
    public String toDeMemListPage(Model model){
        PageInfo list = adminService.CardEnd(1,PAGE_SIZE);
        model.addAttribute("memInfo",list);
        return "adminDeMem_list";
    }
    //2.1.1分页ajax
    @RequestMapping("/ajaxPage3")
    @ResponseBody
    public void ajaxPage3(int page, HttpSession session){
        //取得当前page参数页面数据
        PageInfo info = adminService.CardEnd(page,PAGE_SIZE);
        session.setAttribute("memInfo",info);
    }
    //2.1.2彻底删除
    @RequestMapping("/deleteByid")
    @ResponseBody
    public String deleteByid(Integer memid){
        int row = adminService.deleteByid(memid);
        if (row==1){
            return "ok";
        }else {
            return "no";
        }
    }
    //批量删除，暂时的
    @RequestMapping("/delBatch0")
    @ResponseBody
    public String delBatch0(String memids){
        int row = adminService.delBatch(memids);
        if (row != 0){
            return "ok";
        }else {
            return "no";
        }
    }
    //批量删除，彻底的
    @RequestMapping("/delBatch")
    @ResponseBody
    public String delBatch(String memids){
        int row = adminService.delBatch2(memids);
        if (row != 0){
            return "ok";
        }else {
            return "no";
        }
    }
    //2.1.3恢复
    @RequestMapping("/editByStates1")
    @ResponseBody
    public String editByStates1(Integer memid){
        int row = adminService.editByStates1(memid);
        if (row==1){
            return "ok";
        }else {
            return "no";
        }
    }
    //2.1.4多条件查找删除的会员
    @RequestMapping("/adminFindDeMem")
    @ResponseBody
    public void adminFindDeMem(String memname, HttpSession session){
        PageInfo info = adminService.splitpage3(1,PAGE_SIZE,memname);
        //取得当前page参数页面数据
        session.setAttribute("memInfo",info);
    }
//3.教练信息管理
    //1.跳转到会员列表，分页显示
    @RequestMapping("/toCoachList")
    public String toCoachList(Model model){
        PageInfo info = adminService.splitCoach(1,PAGE_SIZE);
        model.addAttribute("coachInfo",info);
        return "adminCoach_list";
    }
    //2.分页ajax
    @RequestMapping("/ajaxsplitCoach")
    @ResponseBody
    public void ajaxsplitcoach(int page, HttpSession session){
        PageInfo info = adminService.splitCoach(page,PAGE_SIZE);
        session.setAttribute("coachInfo",info);
    }
    //3.跳转到暂时教练列表，分页显示
    @RequestMapping("/toDeCoachList")
    public String toDeCoachList(Model model){
        PageInfo info = adminService.splitDeCoach(1,PAGE_SIZE);
        model.addAttribute("coachInfo",info);
        return "adminDeCoach_list";
    }
    //4.暂时删除教练
    @RequestMapping("/deCoSt0")
    @ResponseBody
    public String deCoSt0(Integer coachid){
        int row = adminService.deCoSt0(coachid);
        if (row==1){
            return "ok";
        }else {
            return "no";
        }
    }
    //批量删除，暂时的
    @RequestMapping("/delCoachBatch0")
    @ResponseBody
    public String delCoachBatch0(String coachids){
        int row = adminService.delBatchCoach(coachids);
        if (row != 0){
            return "ok";
        }else {
            return "no";
        }
    }
    //批量删除，彻底的
    @RequestMapping("/delCoachBatch1")
    @ResponseBody
    public String delCoachBatch1(String coachids){
        int row = adminService.delBatchCoach1(coachids);
        if (row != 0){
            return "ok";
        }else {
            return "no";
        }
    }
    //5.编辑教练信息
    @RequestMapping("/editShowCo")
    @ResponseBody
    public void editShowCo(Integer coachid,HttpSession session){
        TCoach coach = adminService.editShowCo(coachid);
//        System.out.println(coach);
        session.setAttribute("editShowCo",coach);
    }
    //2.1.5确定修改
    @RequestMapping("/editCoConfirm")
    public String editCoConfirm(TCoach coach,HttpSession session){
        //销毁session
        session.removeAttribute("editShowCo");
//        System.out.println(coach);
        //修改方法
        int row = adminService.editCoach(coach);
//        System.out.println("row:"+row);
        return "adminCoach-edit";
    }

    //2.1.2彻底删除
    @RequestMapping("/deleteCo")
    @ResponseBody
    public String deleteCo(Integer coachid){
        int row = adminService.deleteCo(coachid);
        if (row==1){
            return "ok";
        }else {
            return "no";
        }
    }

    //2.1.3恢复
    @RequestMapping("/deCoSt1")
    @ResponseBody
    public String deCoSt1(Integer coachid){
        int row = adminService.deCoSt1(coachid);
        if (row==1){
            return "ok";
        }else {
            return "no";
        }
    }
//4.会员卡信息管理
    //1.跳转会员卡页面
    @RequestMapping("/tocardListPage")
    public String tocardListPage(Model model){
        List list = adminService.findAllCard();
        model.addAttribute("cardList",list);
        return "adminCard_list";
    }
    //2.条件查询会员卡
    @RequestMapping("/adminFindCard")
    @ResponseBody
    public String adminFindCard(String cardname){
        List list = adminService.findByName(cardname);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(list);
            return json;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
    //3.修改回显
    @RequestMapping("/editCardShow")
    @ResponseBody
    public void editCardShow(String cardid,HttpSession session){
        TCard card = adminService.findById(Integer.valueOf(cardid));
        session.setAttribute("editCardShow",card);
    }
    //4.修改
    @RequestMapping("/editCard")
    public String editCard(TCard card){
        int row = adminService.editCard(card);
        return "adminCard-edit";
    }
    //5.删除
    @RequestMapping("/delCard")
    @ResponseBody
    public String delCard(String cardid){
        int row = adminService.delCardByid(Integer.valueOf(cardid));
        if (row==1){
            return "ok";
        }
        return "no";
    }
    //6.添加会员卡
    @RequestMapping("/addCard")
    public String addCard(TCard card){
        int row = adminService.addCard(card);
        if (row==1){
            return "addSuccess";
        }
        return "adminCard-add";
    }
//5.课程信息管理
    //1.跳转课程页面
    @RequestMapping("/toCourseListPage")
    public String toCourseListPage(Model model,HttpSession session){
        PageInfo info = adminService.courseSplit(1,PAGE_SIZE);
        model.addAttribute("courseInfo",info);
        List list = adminService.allCoach();
        List list1 = adminService.allClassroom();
        session.setAttribute("editCoachShow",list);
        session.setAttribute("editRoomShow",list1);
        return "adminCourse_list";
    }
    //2.分页ajax
    @RequestMapping("/ajaxCourse")
    @ResponseBody
    public void ajaxCourse(int page,HttpSession session){
        PageInfo info = adminService.courseSplit(page,PAGE_SIZE);
        session.setAttribute("courseInfo",info);
    }
    //3.暂时删除课程
    @RequestMapping("/delCourse")
    @ResponseBody
    public String delCourse(int courseid){
        int row = adminService.delCourse(courseid);
        if (row==1){
            return "ok";
        }
        return "no";
    }
    //4.修改回显
    @RequestMapping("/editCourseShow")
    @ResponseBody
    public void editCourseShow(int courseid,HttpSession session){
        TCourse course = adminService.selectCourseById(courseid);
        session.setAttribute("editCourseShow",course);
    }
    //5.确认修改
    @RequestMapping("/editCourse")
    public String editCourse(TCourse course){
//        System.out.println(course);
        int row = adminService.updateCourse(course);
        return "adminCourse-edit";
    }
    //6.查询课程
    @RequestMapping("/findCourse")
    @ResponseBody
    public void findCourse(String courseweek,String classroom,String coursename,HttpSession session){
        PageInfo info = adminService.findCourse(1,PAGE_SIZE,courseweek,classroom,coursename);
        session.setAttribute("courseInfo",info);
    }
    //7.多条件查询分页
    @RequestMapping("/findCourseAplit")
    @ResponseBody
    public void findCourseAplit(int page,String courseweek,String classroom,String coursename,HttpSession session){
        PageInfo info = adminService.findCourse(page,PAGE_SIZE,courseweek,classroom,coursename);
        session.setAttribute("courseInfo",info);
    }
    //8.添加课程
    @RequestMapping("/addCourse")
    public String addCourse(TCourse course){
        int row = adminService.addCourse(course);
        return "addSuccess";
    }
    //跳转课程删除页面
    @RequestMapping("/toDeCourseListPage")
    public String toDeCourseListPage(Model model){
        PageInfo info = adminService.deCourseSplit(1,PAGE_SIZE);
        model.addAttribute("deCourseInfo",info);
        return "adminDeCourse_list";
    }
    //删除界面分页
    @RequestMapping("/toDeCourseSplit")
    @ResponseBody
    public void toDeCourseSplit(int page,HttpSession session){
        PageInfo info = adminService.deCourseSplit(page,PAGE_SIZE);
        session.setAttribute("deCourseInfo",info);
    }
    // 删除界面查询
    @RequestMapping("/findDeCourse")
    @ResponseBody
    public void findDeCourse(String coursename,HttpSession session){
        PageInfo info = adminService.findCourseName(1,PAGE_SIZE,coursename);
        session.setAttribute("deCourseInfo",info);
    }
    @RequestMapping("/findDeCourseSplit")
    @ResponseBody
    public void findDeCourseSplit(int page,String coursename,HttpSession session){
        PageInfo info = adminService.findCourseName(page,PAGE_SIZE,coursename);
        session.setAttribute("deCourseInfo",info);
    }
    //3.彻底删除课程
    @RequestMapping("/delCourse0")
    @ResponseBody
    public String delCourse0(int courseid){
        int row = adminService.delCourse0(courseid);
        if (row==1){
            return "ok";
        }
        return "no";
    }

    //3.恢复课程
    @RequestMapping("/delCourse1")
    @ResponseBody
    public String delCourse1(int courseid){
        int row = adminService.delCourse1(courseid);
        if (row==1){
            return "ok";
        }
        return "no";
    }

    //批量删除，彻底的
    @RequestMapping("/delCourseBatch")
    @ResponseBody
    public String delCourseBatch(String coachids){
        int row = adminService.delCourseBatch(coachids);
        if (row != 0){
            return "ok";
        }else {
            return "no";
        }
    }
    //批量删除，暂时的
    @RequestMapping("/delCourseBatch0")
    @ResponseBody
    public String delCourseBatch0(String coachids){
        int row = adminService.delCourseBatch0(coachids);
        if (row != 0){
            return "ok";
        }else {
            return "no";
        }
    }
//6.报名信息管理
    //1.跳转报名信息页面,分页显示
    @RequestMapping("/toApplyPage")
    public String toApplyPage(Model model){
        PageInfo info = adminService.allApply(1,PAGE_SIZE);
        model.addAttribute("applyInfo",info);
        return "adminApply_list";
    }
    //2.分页ajax
    @RequestMapping("/ajaxApply")
    @ResponseBody
    public void ajaxApply(int page,HttpSession session){
        PageInfo info = adminService.allApply(page,PAGE_SIZE);
        session.setAttribute("applyInfo",info);
    }
    //3.删除
    @RequestMapping("/delApply")
    @ResponseBody
    public String delApply(Integer applyid){
        int row = adminService.delApply0(applyid);
        if (row==1){
            return "ok";
        }else {
            return "no";
        }
    }
    //4.修改回显
    @RequestMapping("/editApplyShow")
    @ResponseBody
    public void editApplyShow(int applyid,HttpSession session){
        TApply apply = adminService.editApplyShow(applyid);
        session.setAttribute("editApplyShow",apply);
    }
    //5.确认修改
//    @RequestMapping("/editCourse")
//    public String editApply(TCourse course){
//        System.out.println(course);
//        int row = adminService.updateCourse(course);
//        return "adminCourse-edit";
//    }
    //6.查询报名信息
    @RequestMapping("/findApply")
    @ResponseBody
    public void findApply(String memname, String coursename,HttpSession session){
        PageInfo info = adminService.findApplyCluase(1,PAGE_SIZE,memname,coursename);
        session.setAttribute("applyInfo",info);
    }
    //7.查询报名信息分页
    @RequestMapping("/findApplySplit")
    @ResponseBody
    public void findApplySplit(int page,String memname,String coursename,HttpSession session){
        System.out.println("0:"+page+",1:"+memname+",2:"+coursename);
        PageInfo info = adminService.findApplyCluase(page,PAGE_SIZE,memname,coursename);
        session.setAttribute("applyInfo",info);
    }
    //8.添加报名信息
    @RequestMapping("/addApply")
    @ResponseBody
    public String addApply(String memname,String coursename){
        boolean bo = adminService.checkAdd(memname,coursename);
        if (bo){
            int row = adminService.addApply(memname,coursename);//重复的课程无法添加成功
            return "ok";
        }else {
            return "no";
        }
    }
//7.教室信息管理
    //1.跳转教室信息页面,分页显示
    @RequestMapping("/toClassroomPage")
    public String toClassroomPage(Model model){
        PageInfo info = adminService.allClassroom(1,PAGE_SIZE);
        model.addAttribute("classroomInfo",info);
        return "adminClassroom_list";
    }
    //2.分页ajax
    @RequestMapping("/ajaxClassroom")
    @ResponseBody
    public void ajaxClassroom(int page,HttpSession session){
        PageInfo info = adminService.allClassroom(page,PAGE_SIZE);
        session.setAttribute("classroomInfo",info);
    }
    //3.删除
    @RequestMapping("/delClassroom")
    @ResponseBody
    public String delClassroom(Integer classroomid){
        int row = adminService.delClassroom(classroomid);
        if (row==1){
            return "ok";
        }else {
            return "no";
        }
    }
    //4.修改回显
    @RequestMapping("/editClassroomShow")
    @ResponseBody
    public void editClassroomShow(Integer classroomid,HttpSession session){
        TClassroom classroom = adminService.classroomShow(classroomid);
        session.setAttribute("classroomShow",classroom);
    }
    //5.修改教室信息
    @RequestMapping("/editClassroom")
    public String editClassroom(TClassroom classroom){
        int row = adminService.editClassroom(classroom);
        return "addSuccess";
    }
    //6.添加
    @RequestMapping("/addClassroom")
    public String addClassroom(TClassroom classroom){
        int row = adminService.addClassroom(classroom);
        return "addSuccess";
    }
    //7.查询教室
    @RequestMapping("/findClassroom")
    @ResponseBody
    public void findClassroom(String classroomname,HttpSession session){
        PageInfo info = adminService.findClassroom(1,PAGE_SIZE,classroomname);
        session.setAttribute("classroomInfo",info);
    }
    //8.查询教室,分页
    @RequestMapping("/findClassroomSplit")
    @ResponseBody
    public void findClassroomSplit(int page,String classroomname,HttpSession session){
        PageInfo info = adminService.findClassroom(page,PAGE_SIZE,classroomname);
        session.setAttribute("classroomInfo",info);
    }
//8.器材信息管理
    //1.跳转器材管理页面
    @RequestMapping("/toEquPage")
    public String toEquPage(Model model){
        PageInfo info = adminService.allEqu(1,PAGE_SIZE);
        model.addAttribute("equInfo",info);
        return "adminEquipment_list";
    }
    //2.分页ajax
    @RequestMapping("/ajaxEqu")
    @ResponseBody
    public void ajaxEqu(int page,HttpSession session){
        PageInfo info = adminService.allEqu(page,PAGE_SIZE);
        session.setAttribute("equInfo",info);
    }
    //3.暂时删除
    @RequestMapping("/delEqu")
    @ResponseBody
    public String delEqu(Integer equipid){
        int row = adminService.deEqu0(equipid);
        if (row==1){
            return "ok";
        }
        return "no";
    }
    //4.查询
    @RequestMapping("/findEqu")
    @ResponseBody
    public void findEqu(String buyDate,String eqname,HttpSession session){
        PageInfo info = adminService.findEqu(1,PAGE_SIZE,buyDate,eqname);
        session.setAttribute("equInfo",info);
    }
    //5.查询分页
    @RequestMapping("/findEquSplit")
    @ResponseBody
    public void findEquSplit(int page,String buyDate,String eqname,HttpSession session){
        PageInfo info = adminService.findEqu(page,PAGE_SIZE,buyDate,eqname);
        session.setAttribute("equInfo",info);
    }
    //6.修改器材信息回显
    @RequestMapping("/editEquShow")
    @ResponseBody
    public void editEquShow(Integer equipid,HttpSession session){
        TEquipment equipment = adminService.equShow(equipid);
        session.setAttribute("equShow",equipment);
    }
    //5.修改教室信息
    @RequestMapping("/editEqu")
    public String editEqu(TEquipment equipment){
        int row = adminService.editEqu(equipment);
        return "addSuccess";
    }
    //6.添加器材信息
    @RequestMapping("/addEqu")
    public String addEqu(TEquipment equipment){
        int row = adminService.addEqu(equipment);
        return "addSuccess";
    }
    //7.1 跳转器材删除列表
    @RequestMapping("/toDeEquPage")
    public String toDeEquPage(Model model){
        PageInfo info = adminService.allDeEqu(1,PAGE_SIZE);
        model.addAttribute("deEquInfo",info);
        return "adminDeEqu_list";
    }
    //7.2 分页ajax
    @RequestMapping("/ajaxDeEqu")
    @ResponseBody
    public void ajaxDeEqu(int page,HttpSession session){
        PageInfo info = adminService.allDeEqu(page,PAGE_SIZE);
        session.setAttribute("deEquInfo",info);
    }
    //7.3彻底删除
    @RequestMapping("/delEqu1")
    @ResponseBody
    public String delEqu1(Integer equipid){
        int row = adminService.delEqu1(equipid);
        if (row==1){
            return "ok";
        }
        return "no";
    }
    //7.4 恢复
    @RequestMapping("/reEqu")
    @ResponseBody
    public String reEqu(Integer equipid){
        int row = adminService.reEqu(equipid);
        if (row==1){
            return "ok";
        }
        return "no";
    }
}
